package com.example.team.dao.Imp;

import com.example.team.dao.IAttendanceDao;
import com.example.team.entity.Attendance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/****
 * 编写者：王轩
 * 时间：2023/10/27
 * 打卡信息
 * 接口实现
 */
@Repository
public class IAttendanceDaoImpl implements IAttendanceDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //添加方法实现
    @Override
    public int insertAttendance(Attendance attendance) {
        return jdbcTemplate.update(
                "insert into attendance (AttendanceID,EmployeeID,ClockInTime,ClockOutTime,IP)"+
                "values (?,?,?,?,?)"
                ,attendance.getAttendanceID()
                ,attendance.getEmployeeID()
                ,attendance.getClockInTime()
                ,attendance.getClockOutTime()
                ,attendance.getIP());
    }

    //删除方法实现
    @Override
    public int deleteAttendance(Attendance attendance) {
        return jdbcTemplate.update(
                "delete from attendance where AttendanceID=?"
                ,attendance.getAttendanceID());
    }
    //更改时间
    //下班打卡
    @Override
    public int updateTime(Attendance attendance) {
        return jdbcTemplate.update(
                "update  attendance set ClockOutTime=?"+"where EmployeeID=?",
                attendance.getClockOutTime(),attendance.getEmployeeID());
    }

    //更改方法实现
    @Override
    public int update(Attendance attendance) {
        return jdbcTemplate.update("update attendance set " +
                        "ClockInTime=?,ClockOutTime=?,IP=?"+"where AttendanceID=?"
                ,attendance.getClockInTime()
                ,attendance.getClockOutTime()
                ,attendance.getIP()
                ,attendance.getAttendanceID()
        );
    }
    //查询方法实现
    @Override
    public List<Attendance> getAllAttendance(Attendance attendance) {
            return jdbcTemplate.query("select " +
                            "AttendanceID,EmployeeID,ClockInTime,ClockOutTime,IP " +
                            "from attendance",new Object[]{},
                    new BeanPropertyRowMapper<>(Attendance.class));
    }
    //查询单个方法实现
    @Override
    public Attendance getAttendance(Attendance attendance) {
        List<Attendance> result=jdbcTemplate.query("select " +
                        "`AttendanceID`,`EmployeeID`,`ClockInTime`,`ClockOutTime`,`IP`" +
                        "from attendance where AttendanceID='"+attendance.getAttendanceID()+"'",
                new Object[]{},
                new BeanPropertyRowMapper<>(Attendance.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
    //判断员工id是否存在
    @Override
    public List<Attendance> findByEmployeeID(Attendance attendance) {
        return jdbcTemplate.query(
                "select `EmployeeID` from attendance ",new Object[]{},
                new BeanPropertyRowMapper<>(Attendance.class));
    }

    @Override
    public List<Attendance> findByAttendance(Attendance attendance) {
        return jdbcTemplate.query(
                "select `AttendanceID` from attendance ",new Object[]{},
                new BeanPropertyRowMapper<>(Attendance.class));
    }
}
